package com.huitone.smspfm.oldclient.asclient.send;

import java.util.List;
import java.util.concurrent.ExecutorService;

import com.huitone.smspfm.oldclient.logger.ExtLogger;

import com.huitone.smspfm.context.app.AppManager;
import com.huitone.smspfm.context.thread.AppCustomBlockingThreadPool;
import com.huitone.smspfm.oldclient.model.MtSms;
import com.huitone.smspfm.oldclient.store.MtTmpStore;
import com.huitone.smspfm.socket.client.SocketClient;
import com.huitone.smspfm.socket.protocol.sgip.constant.CommandID;
import com.huitone.smspfm.socket.protocol.sgip.constant.Constant;
import com.huitone.smspfm.socket.protocol.sgip.message.msgbody.Submit2;
import com.huitone.smspfm.socket.protocol.sgip.message.msghead.Head;

/**   
* @Title ReSendMsgOldManager.java 
* @Package com.huitone.smspfm.oldclient.module.mt 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年5月26日 上午10:11:53 
* @version V1.0   
*/
public class ReSendMsgOldManager extends Thread {
	
	private final static ExtLogger logger = ExtLogger.create(ReSendMsgOldManager.class);
	
	private final static long TIME_INTEVAL = 60000;
	
	private boolean toSleep = false;
	private long timeToSleep = 1000;
	
	private ExecutorService fixedThreadPool = AppCustomBlockingThreadPool.newBlockingThreadPool(5);
	private ExecutorService fixedThreadPool2 = AppCustomBlockingThreadPool.newBlockingThreadPool(30);
	
	private static ReSendMsgOldManager instance = new ReSendMsgOldManager();
	
	public static ReSendMsgOldManager getInstance() {
		return instance;
	}
	
	private ReSendMsgOldManager() {}

	@Override
	public void run() {
		
		while (AppManager.isRunning() && !this.isInterrupted()) {
			
			fixedThreadPool.execute(new Runnable() {
				
				@Override
				public void run() {
					
					List<MtSms> list = MtTmpStore.getOutTimes(TIME_INTEVAL);
					
					if (!list.isEmpty()) {
						for (MtSms mtSms:list) {
							
							int times = mtSms.getiSendTimes();
							
							if (times <= 3) {
								
								fixedThreadPool2.execute(new Runnable() {
									
									@Override
									public void run() {
										
										Submit2 submit = Submit2.build(mtSms.getMobileno(), mtSms.getSrvNode(), mtSms.getServiceCode(), 
												mtSms.getSendport(), 1, Constant.MessageCodingE.GBK.getValue(), 
												Constant.MessageTypeE.SMS.getValue(), mtSms.getContent(), mtSms.getClientname(), mtSms.getOldserialnos());
										Head head = Head.build(submit.length(), CommandID.SGIP_SUBMIT2, mtSms.getCoderule(), mtSms.getDatetime(), mtSms.getSeqnumber());
										
										try {
											SocketClient.getInstance().sendMsg(head, submit);
										} catch (Exception e) {
											logger.error("重发短信时出错:" + mtSms.toString(), e);
										}
									}
								});
							} else {
								
								MtTmpStore.remove(mtSms.getSerialno());
								
								logger.warn("短信发送失败,重发超过3次:" + mtSms.toString());
								
								/*fixedThreadPool2.execute(new Runnable() {
									
									@Override
									public void run() {
										MtTmpStore.remove(mtSms.getSerialno());
										try {
											FailedMsgFileWriter.getInstance().write(mtSms);
										} catch (IOException e) {
											logger.error("重发线程写发送失败短信发生错误:" + mtSms.toString(), e);
										}
									}
								});*/
							}
						}
						toSleep = false;
					} else {
						toSleep = true;
					}
				}
			});
			
			try {
				if (toSleep) {
					Thread.sleep(timeToSleep);
				}
			} catch (InterruptedException e) {
				break;
			}
		}
		
		if (fixedThreadPool != null) {
			fixedThreadPool.shutdown();
		}
		
		if (fixedThreadPool2 != null) {
			fixedThreadPool2.shutdown();
		}
	}
	
	public void shutdown() {
		if (fixedThreadPool != null) {
			fixedThreadPool.shutdown();
		}
	}

}
